package cn.edu.scau.cmi.houzhicong.abstractFactoryPattern.gui;

import cn.edu.scau.cmi.houzhicong.abstractFactoryPattern.domain.Insurance;
import cn.edu.scau.cmi.houzhicong.abstractFactoryPattern.factory.AbstractFactory;
import cn.edu.scau.cmi.houzhicong.abstractFactoryPattern.factory.PICCFactory;
import cn.edu.scau.cmi.houzhicong.abstractFactoryPattern.factory.PingAnFactory;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class AbstractFactoryController implements Initializable {
    private final String[] insurancesKind = new String[]{"无", "身体受伤", "汽车损坏", "人员伤亡", "多种事故"};
    private final String[] insurancesBrand = new String[]{"无", "PICC","PingAn"};
    @FXML
    private ChoiceBox choice;
    @FXML
    private ChoiceBox brandChoice;
    @FXML
    private Button addPicButton;
    @FXML
    private Button showButton;
    @FXML
    private TextField nameText;
    @FXML
    private TextField stuNumText;
    @FXML
    private ImageView imageView;
    @FXML
    private TextArea infoArea;
    private String type = null;
    private String brand = null;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        choice.setItems(FXCollections.observableArrayList("无", "身体受伤", "汽车损坏", "人员伤亡", "多种事故"));
        choice.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
            type = insurancesKind[newValue.intValue()];
        });


        brandChoice.setItems(FXCollections.observableArrayList("无", "PICC","PingAn"));
        brandChoice.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
            brand = insurancesBrand[newValue.intValue()];
        });

    }

    public void addPic() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择一个图片文件");
        fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Image", "*.jpg", "*.jpeg", "*.png"));
        Stage exStage = null;
        File file = fileChooser.showOpenDialog(exStage);
//        System.out.println(file.getAbsolutePath());
        if (file != null) {
            imageView.setImage(new Image("file:" + file.getAbsolutePath(), true));
        }
    }


    public void createInsurance() {
        String name = nameText.getText();
        String stuNum = stuNumText.getText();
        AbstractFactory factory = null;
        if (brand != null) {
            switch (brand) {
                case "PICC":
                    factory = new PICCFactory();
                    break;
                case "PingAn":
                    factory = new PingAnFactory();
                    break;
            }
        }


        Insurance insurance = null;
        if (factory == null) {
            infoArea.setText("请选择保险公司！");
        } else if(type==null || type.equals("无")) {
            infoArea.setText("请选择保险类型！");
        }else{
            switch (type) {
                case "身体受伤":
                    insurance = factory.createBodyInjury(name, stuNum, imageView.getImage());
                    break;
                case "汽车损坏":
                    insurance = factory.createCarDamage(name, stuNum, imageView.getImage());
                    break;
                case "人员伤亡":
                    insurance = factory.createCasualty(name, stuNum, imageView.getImage());
                    break;
                case "多种事故":
                    insurance = factory.createMultipleAccidents(name, stuNum, imageView.getImage());
                    break;
            }
            infoArea.setText(insurance.toString());
        }
    }

}
